/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dsproject;

import java.io.Serializable;
import java.util.ArrayList;

/**
 *
 * @author Honoré
 */
public class Path implements Serializable {

    Boolean direction;
    ArrayList<Node> path;
    int iterator;

    public enum pathStatus {

        STARTING, PENDING, ANSWERED, COMPLETED
    }

    public Path(ArrayList<Node> mpath) {
        this.path = mpath;
        this.iterator = -1;
        this.direction = true;
    }

    public Path(ArrayList<Node> mpath, boolean twoway) {
        this.path = mpath;
        this.iterator = (twoway ? -1 : mpath.size());
        this.direction = (twoway ? true : false);
    }

    public Node next() {
        if (this.getStatus() != pathStatus.COMPLETED) {
            return direction ? this.path.get(++iterator) : this.path.get(--iterator);
        } else {
            return null;
        }
    }

    public void changeDirection() {
        direction = false;
    }

    public void updatePath(ArrayList<Node> mpath) {
        Boolean occurrence;
        Node currentNode = this.path.get(iterator);
        occurrence = (path.indexOf(currentNode) == iterator);
        this.path = mpath;
        this.iterator = occurrence ? path.indexOf(currentNode) : path.lastIndexOf(currentNode);
    }

    public void printPath() {
        System.out.println("The path from " + path.get(0) + " to " + path.get(path.size() - 1) + " is: ");
        for (Node nd : this.path) {
            System.out.print(nd + ", ");
        }
        System.out.println();
    }

    public pathStatus getStatus() {
        if (iterator == 0) {
            return direction ? pathStatus.STARTING : pathStatus.COMPLETED;
        } else {
            return direction ? pathStatus.PENDING : pathStatus.ANSWERED;
        }
    }

    public Boolean getDirection() {
        return direction;
    }

    public Node getSource() {
        return path.get(0);
    }

    public Node getDestination() {
        return path.get(path.size() - 1);
    }

    public Node getPosition() {
        if(iterator < 0 || iterator >= path.size()){
            return null;
        }
        else return path.get(iterator);
    }

    public Node getNext() {
        int a = direction ? 1 : -1;
        return (iterator + a < 0 ? null : path.get(iterator + a));
    }
}

